package com.xscha.tableforge.controller;

import com.xscha.soyue.common.core.domain.R;
import com.xscha.tableforge.model.dto.NodeDto;
import com.xscha.tableforge.model.vo.NodeMetaVo;
import com.xscha.tableforge.service.NodeService;
import com.xscha.soyue.common.core.utils.BeanMapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/nodes")
public class NodeController {
    
    @Autowired
    private NodeService nodeService;
    
    /**
     * 获取应用下的所有节点
     */
    @GetMapping("/app/{appId}")
    public R<List<NodeMetaVo>> getAppNodes(@PathVariable String appId) {
        return R.ok(nodeService.getAppNodes(appId));
    }
    
    /**
     * 创建节点
     */
    @PostMapping
    public R<NodeMetaVo> createNode(@RequestBody NodeDto nodeDto) {
        NodeMetaVo node = BeanMapperUtils.map(nodeDto, NodeMetaVo.class);
        return R.ok(nodeService.createNode(node));
    }
    
    /**
     * 更新节点
     */
    @PutMapping("/{id}")
    public R<NodeMetaVo> updateNode(@PathVariable Long id, @RequestBody NodeDto nodeDto) {
        NodeMetaVo node = BeanMapperUtils.map(nodeDto, NodeMetaVo.class);
        return R.ok(nodeService.updateNode(node));
    }
    
    /**
     * 删除节点
     */
    @DeleteMapping("/{id}")
    public R<Boolean> deleteNode(@PathVariable Long id) {
        return R.ok(nodeService.deleteNode(id));
    }
    
    /**
     * 移动节点
     */
    @PutMapping("/{id}/move")
    public R<Boolean> moveNode(@PathVariable Long id, @RequestParam String parentId, @RequestParam Double displayOrder) {
        return R.ok(nodeService.moveNode(id, parentId, displayOrder));
    }
} 